//
//  UserLoginViewController.m
//  HuateVideo
//
//  Created by  on 12-5-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UserLoginViewController.h"
#import "UserCell.h"
#import "UserRegiViewController.h"
#import "CommonWebServices.h"
#import "UserLoginResponse.h"

@interface UserLoginViewController ()

@end

@implementation UserLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor=TOOLBAR_TINTCOLOR; 
    self.title=LS(@"登录");
    UIBarButtonItem* close=[[UIBarButtonItem alloc] initWithTitle:LS(@"关闭") style:UIBarButtonItemStylePlain target:self action:@selector(closeModal)];
    self.navigationController.navigationBar.tintColor = TOOLBAR_TINTCOLOR;

    self.navigationItem.rightBarButtonItem=close;
    [close release];
    [self initTableFooter];
}

-(void)closeModal{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

-(void)initTableFooter{
    UITableView* table=[[UITableView alloc] initWithFrame:CGRectMake(10, 30, 300, 200) style:UITableViewStyleGrouped];
    table.delegate=self;
    table.tag=11111;
    table.dataSource=self;
    table.opaque=NO;
    table.allowsSelection=NO;
    table.backgroundColor=[UIColor clearColor];
    table.backgroundView=nil;
    table.scrollEnabled=NO;
    table.separatorStyle=UITableViewCellSelectionStyleNone;
    [self.view addSubview:table];
    [table release];
    
    UIView*footer=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 50)];
    footer.backgroundColor=[UIColor clearColor];
    table.tableFooterView=footer;
    [footer release];
    
    UIButton* login=[UIButton buttonWithType:UIButtonTypeCustom];
    login.frame=CGRectMake(25, 10, 65, 30);
    [login setBackgroundImage:[UIImage imageNamed:@"sizianniu.png"] forState:UIControlStateNormal];
    [login setTitle:LS(@"登录") forState:UIControlStateNormal];
    [login addTarget:self action:@selector(loginPressed) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:login];
    
    UIButton* reg=[UIButton buttonWithType:UIButtonTypeCustom];
    reg.frame=CGRectMake(200, 10, 65, 30);
    [reg setBackgroundImage:[UIImage imageNamed:@"sizianniu.png"] forState:UIControlStateNormal];
    [reg setTitle:LS(@"注册") forState:UIControlStateNormal];
    [reg addTarget:self action:@selector(registerPressed) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:reg];
}

-(void)loginPressed{
    UITableView* t=(UITableView*)[self.view viewWithTag:11111];
    UserCell*cell=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    UserCell*cell1=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    if (cell.cellTextFiled.text.length&&cell1.cellTextFiled.text.length) {
        
        CommonWebServices *webServ = [[CommonWebServices alloc] init];
        webServ.delegate = self;
        NSMutableDictionary *parma = [NSMutableDictionary dictionary];
        [parma setValue:cell.cellTextFiled.text forKey:@"UserName"];
        [parma setValue:cell1.cellTextFiled.text forKey:@"PassWord"];
        [webServ startRequestWithMode:@"UserInfo" Action:@"UserLogin" Parma:parma Class:@"UserLoginResponse" RootXML:@"UserLoginResponse" resultIsArray:NO];
        [webServ release];
        
    }else {
        UIAlertView* al=[[UIAlertView alloc] initWithTitle:LS(@"提示") message:LS(@"请输入用户名及密码") delegate:nil cancelButtonTitle:LS(@"确定") otherButtonTitles: nil];
        [al show];
        [al release];
    }
}
-(void)registerPressed{
    UserRegiViewController*reg=[[UserRegiViewController alloc] init];
    [self.navigationController pushViewController:reg animated:YES];
    [reg release];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString*identy=@"cell";
    UserCell*cell=(UserCell*)[tableView dequeueReusableCellWithIdentifier:identy];
    if (cell==nil) {
        cell=[[UserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
        cell.cellTextFiled.delegate=self;
    }
    if (indexPath.row==0) {
        cell.cellLab.text=[NSString stringWithFormat:@"%@:",LS(@"用户名")];
    }else {
        cell.cellLab.text=[NSString stringWithFormat:@"%@:",LS(@"密码")];
        cell.cellTextFiled.returnKeyType=UIReturnKeyDone;
        cell.cellTextFiled.secureTextEntry=YES;
    }
    
    return  cell;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    UITableView* t=(UITableView*)[self.view viewWithTag:11111];
    UserCell*cell1=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    if (textField==cell1.cellTextFiled) {
        t.center=CGPointMake(t.center.x, t.center.y-20);
    }else {
    }
    
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    UITableView* t=(UITableView*)[self.view viewWithTag:11111];
    UserCell*cell=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
     UserCell*cell1=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    if (textField==cell.cellTextFiled) {
        [cell1.cellTextFiled becomeFirstResponder];
    }else {
        t.center=CGPointMake(t.center.x, t.center.y+20);
        [textField resignFirstResponder];
    }
    return YES;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - CommonWebServicesDelegate

- (void)onCommonWebServicesWithCustomClass:(id)object {
    UserLoginResponse *userLoginResponse = (UserLoginResponse *)object;
    
    if (userLoginResponse.UserLoginResult == 0) {
        NSLog(@"%d 表示登陆不成功",userLoginResponse.UserLoginResult);
        [self.view showAlert:LS(@"提示") message:LS(@"登陆失败")];
    }
    else if (userLoginResponse.UserLoginResult >= 1) {
        NSLog(@"%d 表示登陆成功,返回值为用户ID",userLoginResponse.UserLoginResult);
        UITableView* t=(UITableView*)[self.view viewWithTag:11111];
        UserCell*cell=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        [UserDefaults setObject:cell.cellTextFiled.text forKey:USERNAME_STORE_KEY];
        
        UserCell*cell1=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        NSString*pwd=cell1.cellTextFiled.text;
        [UserDefaults setObject:pwd forKey:PASSWORD_STORE_KEY];
                [[NSNotificationCenter defaultCenter] postNotificationName:USERLOGIN_ACTION object:cell.cellTextFiled.text];
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
    else if (userLoginResponse.UserLoginResult == -1) {
        NSLog(@"%d 表示没有权限操作",userLoginResponse.UserLoginResult);
        [self.view showAlert:LS(@"提示") message:LS(@"没有权限操作")];
    }
    else if (userLoginResponse.UserLoginResult == -100) {
        NSLog(@"%d 表示传入参数非法",userLoginResponse.UserLoginResult);
        [self.view showAlert:LS(@"提示") message:LS(@"传入参数非法")];
    }
}

- (void)onCommonWebServicesFaild
{
    NSLog(@"失败");
}

- (void)openActiovityIndicator {
    //[self.view showProgressText:YES];
}
- (void)closeActiovityIndicator {
    //[self.view showProgressText:NO];
}

@end
